package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.AssetsService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;

@Controller
@RequestMapping("/assets")

public class AssetsAction  implements Serializable{
	private static final Log logger = LogFactory.getLog(AssetsAction.class);

	@Autowired
	private AssetsService assetsService;

	/** 我的资产明细
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/showAssetDetail")
	public void showAssetDetail(HttpServletRequest request, HttpServletResponse response){	
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");   // 用户id
		
		// 用户id为空
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE,
				Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/assets/showAssetDetail 传入参数为空-----userId");
			return;
		}
		
		try {
			// 调用核心接口E330005我的资产明细
			jsonMap = this.assetsService.getAssetDetail(userId);
			
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("/assets/showAssetDetail 获取我的资产明细 异常：" + CommonUtil.printStackTraceToString(e));
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE,
				Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
	
	/** 我的资产总览
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/showAssets")
	public void showAssets(HttpServletRequest request, HttpServletResponse response){	
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");   // 用户id
		
		// 用户id为空
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
				Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/assets/showAssets 传入参数为空-----userId");
			return;
		}
		
		try {
			// 我的资产总览
			jsonMap = this.assetsService.getAssets(userId);
			
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("/assets/showAssets 获取我的资产总览 异常：" + CommonUtil.printStackTraceToString(e));
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, 
				Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
	
	/** 我的累计收益
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/getAccumulatedIncome")
	public void getAccumulatedIncome (HttpServletRequest request, HttpServletResponse response){	
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");   // 用户id
		
		// 用户id为空
		if (StringUtils.isBlank(userId)) {
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, 
				Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.debug("/assets/getAccumulatedIncome 传入参数为空");
			return;
		}
		
		try {
			// 我的累计收益
			jsonMap = assetsService.serachAccumulatedIncome(userId);
			
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			logger.debug("/assets/getAccumulatedIncome 获取我的累计收益 异常：" + CommonUtil.printStackTraceToString(e));
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE,
				Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
}
